package com.apesource.mallproject.dao;

import com.apesource.mallproject.pojo.AdType;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

@Repository
public interface IAdTypeDao {
    //查询广告类型
    @Select("select * from content_category order by id desc")
    public List<AdType> queryAll();

    //新增广告
    @Insert("INSERT INTO content_category VALUES(NULL,#{name},#{status})")
    public void insertAd(@Param("name") String name, @Param("status") Integer status);

    //修改
    @Update("update content_category set name=#{name},status=#{status} where id=#{id}")
    public void updateAd(@Param("name") String name, @Param("status") Integer status, @Param("id") BigInteger id);

    //删除一个
    @Delete("delete from content_category where id = #{id}")
    void delBrand(@Param("id") BigInteger id);

    //批量删除
    @DeleteProvider(type = AdTypeProvider.class, method = "batchDelete")
    int batchDelete(String[] ids);

    class AdTypeProvider {
        public String batchDelete(Map map) {
            String[] ids = (String[]) map.get("array");
            StringBuilder sb = new StringBuilder();
            sb.append("DELETE FROM content_category WHERE id IN (");
            for (int i = 0; i < ids.length; i++) {
                sb.append("'").append(new BigInteger(ids[i]).intValue()).append("'");
                if (i < ids.length - 1)
                    sb.append(",");
            }
            sb.append(")");
            return sb.toString();
        }

    }

}
